{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 一、文件的读写\n",
    "## 1.1 文件的打开\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "#文件打开的通用格式with open(文件路径,打开模式,ecoding=\"操作文件的字符编码\") as 变量名\n",
    "#                    ”执行文件对应的读写操作“\n",
    "with open(\"E:/pyproject/Python_study/python---code/test1\",\"r\",encoding=\"utf-8\") as f:\n",
    "    text=f.read()\n",
    "    print(text)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T11:02:39.289702300Z",
     "start_time": "2024-11-20T11:02:39.276460700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.1.1 文件路径"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 程序与文件在同一文件夹，直接写文件名即可"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "with open(\"test1\",\"r\",encoding=\"utf-8\") as f:\n",
    "    text=f.read()\n",
    "    print(text)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T11:04:03.459422Z",
     "start_time": "2024-11-20T11:04:03.444154700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.1.2打开模式\n",
    "r:只读模式，文件不存在，报错\n",
    "w:只写模式,覆盖写模式，文件不存在则创建，文件存在则覆盖\n",
    "x:创建模式：文件存在，报错，文件不存在则创建\n",
    "a:追加模式,文件不存在则创建，文件存在则在文件末尾追加\n",
    "b:二进制模式,不能单独使用，要配合“rb\",\"wt\",\"at\"\n",
    "t:文本模式，不能单独使用，要配合“rt\",\"wt\",\"at\"\n",
    "a:追加模式\n",
    "r+:读写模式\n",
    "w+:读写模式\n",
    "a+:读写模式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.1.3 文件编码\n",
    "万国码：utf-8:包含全世界所有国家的字符\n",
    "中文编码：gbk:包含中文的编码"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2 文件的读取\n",
    "### 1.2.1 读取整个文件--read()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "with open(\"test1\",\"r\",encoding=\"utf-8\") as f:\n",
    "    text=f.read()\n",
    "    print(text)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T11:11:33.664645800Z",
     "start_time": "2024-11-20T11:11:33.619897700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2.2 读取一行--readline()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "狗是一种常见的宠物动物，它们通常被视为忠诚的伴侣和朋友。狗有许多不同的品种，每种品种都有自己的特点和特征。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"test1\",\"r\",encoding=\"utf-8\") as f:\n",
    "    text=f.readline()\n",
    "    print(text)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T11:16:05.115369100Z",
     "start_time": "2024-11-20T11:16:05.082126200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2.3 读取多行--readlines()\n",
    "读取的每一行形成一个列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['狗是一种常见的宠物动物，它们通常被视为忠诚的伴侣和朋友。狗有许多不同的品种，每种品种都有自己的特点和特征。\\n', '\\n', '狗的寿命通常在10至17年之间，具体取决于品种和饮食习惯。狗的肉体需要定期维护，包括适当的饮食、运动和免疫系统的保健。\\n', '\\n', '狗的社交性和协作能力使它们成为了人类的伙伴和助手。它们可以帮助残疾人士行走、提供帮助和支持，并且在家庭中扮演着重要的角色。\\n', '\\n', '狗的文化和习俗也在不断发展和演变。它们参与了许多艺术、文化和娱乐活动，并且在社交和职业领域也有着广泛的应用。\\n', '\\n', '狗是一种非常有趣和亲和的动物，它们可以带来无尽的快乐和愉悦。无论是在家庭中还是在公共场所，狗都是人们的伙伴和朋友。']\n"
     ]
    }
   ],
   "source": [
    "with open(\"test1\",\"r\",encoding=\"utf-8\") as f:\n",
    "    text=f.readlines()\n",
    "    print(text)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T11:16:01.854476600Z",
     "start_time": "2024-11-20T11:16:01.839783500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.2.4 使用可迭代对象进行读取"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "with open(\"test1\",\"r\",encoding=\"utf-8\") as f:\n",
    "    for line in f:#f是可迭代对象,每次迭代读取一行内容\n",
    "        print(line)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.2.5 二进制文件"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xe7\\x8b\\x97\\xe6\\x98\\xaf\\xe4\\xb8\\x80\\xe7\\xa7\\x8d\\xe5\\x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9b\\xe7\\x9a\\x84\\xe5\\xba\\x94\\xe7\\x94\\xa8\\xe3\\x80\\x82\\r\\n\\r\\n\\xe7\\x8b\\x97\\xe6\\x98\\xaf\\xe4\\xb8\\x80\\xe7\\xa7\\x8d\\xe9\\x9d\\x9e\\xe5\\xb8\\xb8\\xe6\\x9c\\x89\\xe8\\xb6\\xa3\\xe5\\x92\\x8c\\xe4\\xba\\xb2\\xe5\\x92\\x8c\\xe7\\x9a\\x84\\xe5\\x8a\\xa8\\xe7\\x89\\xa9\\xef\\xbc\\x8c\\xe5\\xae\\x83\\xe4\\xbb\\xac\\xe5\\x8f\\xaf\\xe4\\xbb\\xa5\\xe5\\xb8\\xa6\\xe6\\x9d\\xa5\\xe6\\x97\\xa0\\xe5\\xb0\\xbd\\xe7\\x9a\\x84\\xe5\\xbf\\xab\\xe4\\xb9\\x90\\xe5\\x92\\x8c\\xe6\\x84\\x89\\xe6\\x82\\xa6\\xe3\\x80\\x82\\xe6\\x97\\xa0\\xe8\\xae\\xba\\xe6\\x98\\xaf\\xe5\\x9c\\xa8\\xe5\\xae\\xb6\\xe5\\xba\\xad\\xe4\\xb8\\xad\\xe8\\xbf\\x98\\xe6\\x98\\xaf\\xe5\\x9c\\xa8\\xe5\\x85\\xac\\xe5\\x85\\xb1\\xe5\\x9c\\xba\\xe6\\x89\\x80\\xef\\xbc\\x8c\\xe7\\x8b\\x97\\xe9\\x83\\xbd\\xe6\\x98\\xaf\\xe4\\xba\\xba\\xe4\\xbb\\xac\\xe7\\x9a\\x84\\xe4\\xbc\\x99\\xe4\\xbc\\xb4\\xe5\\x92\\x8c\\xe6\\x9c\\x8b\\xe5\\x8f\\x8b\\xe3\\x80\\x82'\n"
     ]
    }
   ],
   "source": [
    "with open(\"test1\",\"rb\") as f:\n",
    "    text=f.read()\n",
    "    print(text)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T11:47:53.372879500Z",
     "start_time": "2024-11-20T11:47:53.358601700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2 文件的写入\n",
    "### 1.2.1 向文件写入一个字符串或者字节流"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "with open(\"test2-write.txt\",\"w\",encoding=\"utf-8\") as f:\n",
    "    f.write(\"python world\\n\")\n",
    "    f.write(\"nice to meet you\\n\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T11:54:39.813591500Z",
     "start_time": "2024-11-20T11:54:39.797345200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.2.2 追加模式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "with open(\"test2-write.txt\",\"a\",encoding=\"utf-8\") as f :\n",
    "    f.write(\"dwdocker\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T11:27:31.638510600Z",
     "start_time": "2024-11-21T11:27:31.621055600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.2.3将一个元素为字符串的列表整体写入文件——f.writelines()\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "ls = [\"wsd\",\"abc\",\"def\"]\n",
    "with open (\"test2-write.txt\",\"w\",encoding=\"utf-8\") as f:\n",
    "    f.writelines(ls)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T11:31:05.901107300Z",
     "start_time": "2024-11-21T11:31:05.882111300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.3 既读又写\n",
    "### 1.3.1 “r+”\n",
    "如果文件名不存在，则报错\n",
    "指针在开始\n",
    "要把指针移到末尾才能开始写，否则会覆盖前面内容"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "with open (\"test2-write.txt\",\"r+\",encoding=\"utf-8\") as f:\n",
    "    f.seek(0,2)#参数：第一个参数：偏移字节数，第二个参数：位置（0表示从头开始，1表示从当前位置开始，2表示从尾开始，）\n",
    "    f.write(\"python world\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T11:43:30.137096900Z",
     "start_time": "2024-11-21T11:43:30.118709100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.3.2 “w+”\n",
    "如果文件名不存在，则创建文件\n",
    "若文件存在则直接覆盖前面内容"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "with open (\"test2-write.txt\",\"w+\",encoding=\"utf-8\") as f:\n",
    "    f.seek(0,2)\n",
    "    f.write(\"python world1\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T11:44:59.570799200Z",
     "start_time": "2024-11-21T11:44:59.553275200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.3.3 “a+”\n",
    "如果文件名不存在，则创建文件\n",
    "若文件存在则指针默认在末尾位置，在文件末尾追加内容"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "with open (\"test2-write.txt\",\"a+\",encoding=\"utf-8\") as f:\n",
    "    f.seek(0,2)\n",
    "    f.write(\"python world2\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T11:47:10.444640800Z",
     "start_time": "2024-11-21T11:47:10.424065200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.4 数据的存储和读取\n",
    "### 1.4.1csv格式\n",
    "由逗号将数据分开的字符序列，可以有Excel打开"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Name', 'Age', 'City']\n",
      "['John', '25', 'New York']\n",
      "['Alice', '30', 'San Francisco']\n",
      "['Bob', '35', 'Chicago']\n"
     ]
    }
   ],
   "source": [
    "##读取\n",
    "with open (\"test3_csv.csv\",\"r\",encoding=\"utf-8\") as f:\n",
    "    ls = []\n",
    "    for line in f:\n",
    "        ls.append(line.strip(\"\\n\").split(\",\"))#去掉换行符用逗号分割\n",
    "for res in ls:\n",
    "    print(res)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T11:53:32.141933800Z",
     "start_time": "2024-11-21T11:53:32.088840600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "##写入\n",
    "ls = [[\"John\",'25',\"New York\"],[\"Alice\",'30',\"San Francisco\"],[\"Bob\",'35',\"Chicago\"]]\n",
    "with open (\"test3_csv.csv\",\"w\",encoding=\"utf-8\") as f:\n",
    "   for line in ls:\n",
    "       f.write(\",\".join(line))\n",
    "       f.write(\"\\n\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T11:57:05.734769100Z",
     "start_time": "2024-11-21T11:57:05.709797900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.4.2json格式\n",
    "常用来存储字典类型数据\n",
    "写入——json.dumps()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "##写入——json.dumps()\n",
    "import json\n",
    "ls = [{\"name\":\"John\",\"age\":25,\"city\":\"New York\"},{\"name\":\"Alice\",\"age\":30,\"city\":\"San Francisco\"},{\"name\":\"Bob\",\"age\":35,\"city\":\"Chicago\"}]\n",
    "with open (\"test4_json.json\",\"w\",encoding=\"utf-8\") as f:\n",
    "   f.write(json.dumps(ls))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T12:00:23.638976900Z",
     "start_time": "2024-11-21T12:00:23.625060900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 'John', 'age': 25, 'city': 'New York'}, {'name': 'Alice', 'age': 30, 'city': 'San Francisco'}, {'name': 'Bob', 'age': 35, 'city': 'Chicago'}]\n"
     ]
    }
   ],
   "source": [
    "##读取\n",
    "import json\n",
    "with open (\"test4_json.json\",\"r\",encoding=\"utf-8\") as f:\n",
    "   ls = json.loads(f.read())\n",
    "   print(ls)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T12:02:14.454134700Z",
     "start_time": "2024-11-21T12:02:14.424570400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 二、异常处理"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.1 常见异常处理\n",
    "### 1.除0运算——ZeroDivisionError"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "1/0"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2找不到文件错误———FileNotFoundError\n",
    "### 3.值错误——ValueError\n",
    "传人一个调用者无法理解的值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '1.3'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[11], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m s\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m1.3\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m----> 2\u001B[0m n\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;43mint\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m)\u001B[49m\n",
      "\u001B[1;31mValueError\u001B[0m: invalid literal for int() with base 10: '1.3'"
     ]
    }
   ],
   "source": [
    "s=\"1.3\"\n",
    "n=int(s)##无法把1.3转为int"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T12:04:48.925655400Z",
     "start_time": "2024-11-21T12:04:48.850657500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4.索引错误——IndexError"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mIndexError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[12], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m ls\u001B[38;5;241m=\u001B[39m[\u001B[38;5;241m1\u001B[39m,\u001B[38;5;241m2\u001B[39m,\u001B[38;5;241m3\u001B[39m]\n\u001B[1;32m----> 2\u001B[0m \u001B[43mls\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m]\u001B[49m\n",
      "\u001B[1;31mIndexError\u001B[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "ls=[1,2,3]\n",
    "ls[3]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T12:06:42.508717200Z",
     "start_time": "2024-11-21T12:06:42.454846800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.类型错误——TypeError"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[14], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[38;5;241;43m1\u001B[39;49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43m3\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\n",
      "\u001B[1;31mTypeError\u001B[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "1+\"3\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T12:07:43.693246900Z",
     "start_time": "2024-11-21T12:07:43.657812500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.2 异常的处理\n",
    "### 1.try...except...finally\n",
    "如果try语句中有异常，则执行except中的语句，否则执行finally中的语句"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "除数不能为0\n",
      "程序执行完毕\n"
     ]
    }
   ],
   "source": [
    "#单分支\n",
    "x = 10\n",
    "y = 0\n",
    "try:\n",
    "    z = x/y\n",
    "except ZeroDivisionError:\n",
    "    print(\"除数不能为0\")\n",
    "finally:\n",
    "    print(\"程序执行完毕\")\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T12:09:24.867942600Z",
     "start_time": "2024-11-21T12:09:24.826878400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#多分枝\n",
    "x = 10\n",
    "y = 0\n",
    "try:\n",
    "    z = x/y\n",
    "except ZeroDivisionError:\n",
    "    print(\"除数不能为0\")\n",
    "except ValueError:\n",
    "    print(\"值错误\")\n",
    "finally:\n",
    "    print(\"程序执行完毕\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "division by zero\n"
     ]
    }
   ],
   "source": [
    "##万能异常Exception\n",
    "x = 10\n",
    "y = 0\n",
    "try:\n",
    "    z = x/y\n",
    "except Exception as e:\n",
    "    print(e)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-21T12:12:20.897565700Z",
     "start_time": "2024-11-21T12:12:20.879575700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.try...except...else\n",
    "如果try语句执行，则else中语句也执行"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "x = 10\n",
    "y = 0\n",
    "try:\n",
    "    z = x/y\n",
    "except ZeroDivisionError:\n",
    "    print(\"除数不能为0\")\n",
    "else:\n",
    "    print(\"程序执行完毕\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.try...except...finally\n",
    "无论try语句中有没有异常，finally中的语句都会执行"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "x = 10\n",
    "y = 0\n",
    "try:\n",
    "    z = x/y\n",
    "except ZeroDivisionError:\n",
    "    print(\"除数不能为0\")\n",
    "finally:\n",
    "    print(\"程序执行完毕\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
